package com.heihei.serviceimpl.test;

import com.heihei.common.response.ResponseVO;
import com.heihei.operator.JedisOperator;
import com.heihei.service.test.RedisTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;

@Slf4j
@Service
public class RedisTestServiceImpl implements RedisTestService {

    @Resource
    private JedisOperator jedisOperator;

    /**
     * redis字符串操作示例
     *
     * @author duanhf
     * 2021年12月24日 17:32:14
     */
    @Override
    public ResponseVO<String> redisStrTest() {
        Jedis jedis = this.jedisOperator.getJedis();
        try {
            String keyName = "name";
            //设值
            jedis.set(keyName,"林青霞");
            log.info("{} -- {}", keyName, jedis.get("name"));
            //拼接
            jedis.append(keyName, " 张曼玉");
            log.info("{} -- {}", keyName, jedis.get("name"));

            //删除
            jedis.del(keyName);
            log.info("{} -- {}", keyName, jedis.get("name"));

            //设置多个键值对
            jedis.mset("name","liuling","age","23","qq","476777XXX");
            log.info("name:{}, age:{}, qq:{}", jedis.get("name"), jedis.get("age"), jedis.get("qq"));
            //age +1
            jedis.incr("age");
            log.info("name:{}, age:{}, qq:{}", jedis.get("name"), jedis.get("age"), jedis.get("qq"));
        } finally {
            this.jedisOperator.returnResource(jedis);
            return ResponseVO.dealSuccess();
        }
    }
}
